﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using QuanLyBus.Model;

namespace QuanLyBus
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        QL_TuyenDataContext db = new QL_TuyenDataContext(App.constr);
        List<ds_tuyen> t = new List<ds_tuyen>();
        ds_tuyen item = new ds_tuyen();
        public MainPage()
        {
            InitializeComponent();
            //var t = db.dstuyens.ToList();
           // foreach (var item in t)
            //{
             //   MessageBox.Show(item.ten_tuyen_den);
            //}
            list_bus.ItemsSource = db.ds_tuyens.ToList();
            //dstuyen t = db.dstuyens.Single(p=>p.id_tuyen == 1);
            //list_bus.Items.Add(t);
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Them.xaml",UriKind.Relative));
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            if (list_bus.Items.Count != 0)
            {
                if (App.id != -1)
                {
                    this.NavigationService.Navigate(new Uri("/Sua.xaml", UriKind.Relative));
                }
                else
                {
                    MessageBox.Show("Bạn chưa chọn tuyến, vui lòng chọn...");
                }
            }
            else
            {
                MessageBox.Show("Không có dữ liệu để sửa...");
            }
        }

        private void list_bus_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                item = list_bus.SelectedItem as ds_tuyen;
            App.id = item.id_tuyen;
            }
            catch 
            {
                
                list_bus.SelectedIndex=-1;
            }
            
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (list_bus.Items.Count != 0)
            {
                MessageBoxResult rw = MessageBox.Show("Bạn có thực sự muốn xóa", "Thông báo", MessageBoxButton.OKCancel);
                if (rw == MessageBoxResult.OK)
                {
                    xoa();
                    MessageBox.Show("xóa thành công...");
                }                               
                
            }
            else
            {
                MessageBox.Show("Dữ liệu trống không thể xóa...");
            }
        }
        private void xoa()
        {
            //MessageBox.Show(App.id_t.ToString());
            var c = db.tram_thuoc_tuyens.Where(p => p.id_tram == App.id_t);
            foreach (tram_thuoc_tuyen i in c)
            {
                db.tram_thuoc_tuyens.DeleteOnSubmit(i);
                db.SubmitChanges();  
            }           
            db.ds_tuyens.DeleteOnSubmit(item);
            db.SubmitChanges();            
            Load();
        }
        private void Load()
        {
            t = db.ds_tuyens.ToList();
            this.list_bus.ItemsSource = t;
        }

        private void btn_view_Click(object sender, EventArgs e)
        {
            if (App.id != -1)
            {
                this.NavigationService.Navigate(new Uri("/xem_lo_trinh.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn tuyến, vui lòng chọn...");
            }
            
        }
    }
}